//
//  ZingToolBar.m
//  iOSTest
//
//  Created by -Xh- on 1/11/13.
//  Copyright (c) 2013 -Xh-. All rights reserved.
//

#import "ZingToolBar.h"
#import "StaticVariables.h"
#import "MyUtilities.h"
#import "SGPageViewController.h"
#import "HomePageViewController.h"

@implementation ZingToolBar{
    ZingToolBarState currentState;
}

@synthesize backButton;
@synthesize forwardButton;
@synthesize delegate;
@synthesize background;
@synthesize multiTabButton;
@synthesize viewFavouriteButton;
@synthesize searchTextButton;
@synthesize showMenuButton;
@synthesize fullScreenButton;
@synthesize pageNumberLB;
@synthesize shareButton;
int TB_OFFSET_X=5;
int TB_OFFSET_Y=0;
int BUTTON_WIDTH=70;
int BUTTON_HEIGHT=70;
int MAX_NUMBER_OFBUTTON=7;

-(void)initialize:(CGRect)viewFrame{
    
    float viewHeight=viewFrame.size.height;
    float viewWidth=viewFrame.size.width;
    
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    
    //Setup Background
    CGRect backgroundRect=CGRectMake(0, 0, viewWidth, viewHeight);
    //UIImage* backgroundImage=[[[StaticVariables shareVariables]imageGallery]toolBarBackground];
    background=[[UIImageView alloc]initWithFrame:backgroundRect];
    background.backgroundColor=[UIColor blackColor];
    background.alpha=0.12f;
    background.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [self addSubview:background];
    self.backgroundColor = [UIColor clearColor];
    
    //Setup BackButton
    backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setTitle:@"" forState:UIControlStateNormal];
    backButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    // backButton.frame=CGRectMake(TB_OFFSET_X, TB_OFFSET_Y, viewHeight-TB_OFFSET_X*2, viewHeight-TB_OFFSET_Y*2);
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    [backButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_bw_nor.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_bw_dis.png"] forState:UIControlStateDisabled];
    backButton.enabled=NO;
    backButton.showsTouchWhenHighlighted=YES;
    [self addSubview:backButton];
    
    
    
    //Setup ForwardButton
    forwardButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [forwardButton setTitle:@"" forState:UIControlStateNormal];
    forwardButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    // float backButtonWidth=backButton.frame.origin.x+backButton.frame.size.width;
    //forwardButton.frame=CGRectMake(backButtonWidth+TB_OFFSET_X, TB_OFFSET_Y, viewHeight-TB_OFFSET_X*2, viewHeight-TB_OFFSET_Y*2);
    [forwardButton addTarget:self action:@selector(forwardAction) forControlEvents:UIControlEventTouchUpInside];
    
    [forwardButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_fw_nor.png"] forState:UIControlStateNormal];
    [forwardButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_fw_dis.png"] forState:UIControlStateDisabled];
    forwardButton.showsTouchWhenHighlighted=YES;
    forwardButton.enabled=NO;
    [self addSubview:forwardButton];
    
    //Setup viewFavouriteButton
    
    viewFavouriteButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [viewFavouriteButton setTitle:@"" forState:UIControlStateNormal];
    viewFavouriteButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    [viewFavouriteButton addTarget:self action:@selector(viewFavouriteAction) forControlEvents:UIControlEventTouchUpInside];
    UIImage* viewFavouriteImage=[UIImage imageNamed:@"ic_browser_toolbar_bookmark_nor.png"];
    [viewFavouriteButton setImage:viewFavouriteImage forState:UIControlStateNormal];
    viewFavouriteButton.enabled=YES;
    viewFavouriteButton.showsTouchWhenHighlighted=YES;
    [self addSubview:viewFavouriteButton];
    
    
    //[self addSubview:searchTextButton];
    
    showMenuButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [showMenuButton setTitle:@"" forState:UIControlStateNormal];
    showMenuButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [showMenuButton addTarget:self action:@selector(showMenuAction) forControlEvents:UIControlEventTouchUpInside];
    [showMenuButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_menu_nor.png"] forState:UIControlStateNormal];
    showMenuButton.enabled=YES;
    showMenuButton.showsTouchWhenHighlighted=YES;
    [self addSubview:showMenuButton];
    
    fullScreenButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [fullScreenButton setTitle:@"" forState:UIControlStateNormal];
    fullScreenButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [fullScreenButton addTarget:self action:@selector(fullScreenAction) forControlEvents:UIControlEventTouchUpInside];
    [fullScreenButton setImage:[UIImage imageNamed:@"ic_browser_menu_fullscreen_nor.png"] forState:UIControlStateNormal];
    fullScreenButton.enabled=YES;
    fullScreenButton.showsTouchWhenHighlighted=YES;
    [self addSubview:fullScreenButton];
    
    shareButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [shareButton setTitle:@"" forState:UIControlStateNormal];
    shareButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [shareButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [shareButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_share_nor.png"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_share_dis.png"] forState:UIControlStateDisabled];
    shareButton.enabled=YES;
    shareButton.showsTouchWhenHighlighted=YES;
    [self addSubview:shareButton];
    
    
    
    multiTabButton=[UIButton buttonWithType:UIButtonTypeCustom];
    //[multiTabButton setTitle:@"" forState:UIControlStateNormal];
    multiTabButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    multiTabButton.titleEdgeInsets = UIEdgeInsetsMake(3, 0, 0, 0);
    multiTabButton.titleLabel.font = [UIFont systemFontOfSize:11];
    [multiTabButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [multiTabButton addTarget:self action:@selector(multiTabAction) forControlEvents:UIControlEventTouchUpInside];
    
    [multiTabButton setBackgroundImage:[UIImage imageNamed:@"ic_browser_toolbar_tabs_nor.png"] forState:UIControlStateNormal];
    //[multiTabButton setImage:multiTabImage forState:UIControlStateNormal];
    multiTabButton.enabled=YES;
    multiTabButton.showsTouchWhenHighlighted=YES;
    [self addSubview:multiTabButton];
    
    
    pageNumberLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];
    pageNumberLB.text = [NSString stringWithFormat:@"%i", 123];
    pageNumberLB.layer.borderColor=[[UIColor blackColor]CGColor];
    pageNumberLB.layer.borderWidth=2;
    //[self addSubview:pageNumberLB];
    
    
    
    NSMutableArray* arrayRect=[[NSMutableArray alloc]init];
    double eachButtonWidth=viewHeight-TB_OFFSET_Y*2;
    double remainingSpaceForAllRect=viewWidth-MAX_NUMBER_OFBUTTON*eachButtonWidth;
    double eachSpace=remainingSpaceForAllRect/(2+MAX_NUMBER_OFBUTTON-1);
    
    for(int i=0;i<MAX_NUMBER_OFBUTTON;i++){
        CGRect iRect=CGRectMake(((i+1)*eachSpace)+i*eachButtonWidth, TB_OFFSET_Y, eachButtonWidth, eachButtonWidth);
        [arrayRect addObject:[NSValue valueWithCGRect:iRect]];
    }
    backButton.frame=[[arrayRect objectAtIndex:0]CGRectValue];
    forwardButton.frame=[[arrayRect objectAtIndex:1]CGRectValue];
    shareButton.frame=[[arrayRect objectAtIndex:2]CGRectValue];
    viewFavouriteButton.frame=[[arrayRect objectAtIndex:3]CGRectValue];
    multiTabButton.frame=[[arrayRect objectAtIndex:MAX_NUMBER_OFBUTTON-3]CGRectValue];
    showMenuButton.frame=[[arrayRect objectAtIndex:MAX_NUMBER_OFBUTTON-2]CGRectValue];
    fullScreenButton.frame=[[arrayRect objectAtIndex:MAX_NUMBER_OFBUTTON-1]CGRectValue];
    // [MyUtilities logRect:viewFavouriteButton.frame withTag:@"favouriteFrame"];
    
    currentState=TB_Normal;
    //urlStack = [[MyStack alloc]init];
}
-(void)updateChrome{
    if(currentState == TB_Normal){
        backButton.enabled = self.browser.canGoBack;
        forwardButton.enabled = self.browser.canGoForward;
        NSString *text = [NSString stringWithFormat:@"%d", self.browser.count];
        [multiTabButton setTitle:text forState:UIControlStateNormal];
        
        if([[self.browser selectedViewController] isKindOfClass:[HomePageViewController class]]){
            HomePageViewController* homeVC = (HomePageViewController*)self.browser.selectedViewController;
            backButton.enabled = NO;
            if(homeVC.lastURL != nil){
                forwardButton.enabled = YES;
            }
        }else{
            backButton.enabled = YES;
        }
    }else{

        
    }
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // Initialization code
        [self initialize:frame];
    }
    return self;
}

-(void)backAction{
    if(currentState==TB_Normal){
        if(delegate && [delegate respondsToSelector:@selector(onPressBackButton)]){
            [delegate onPressBackButton];
        }
    }else{
        if(delegate && [delegate respondsToSelector:@selector(onPressDoneButton)]){
            [self activate:NO];
            [self swapState];
            [delegate onPressDoneButton];
            
            
        }
    }
}

-(void)forwardAction{
    if(currentState==TB_Normal){
        if(delegate && [delegate respondsToSelector:@selector(onPressForwardButton)]){
            [delegate onPressForwardButton];
        }
    }else{
        if(delegate && [delegate respondsToSelector:@selector(onPressOpenClosedTabButton)]){
            [delegate onPressOpenClosedTabButton];
        }
    }
    
}
-(void)searchTextAction{
    if(delegate && [delegate respondsToSelector:@selector(onPressSearchTextButton)]){
        [delegate onPressSearchTextButton];
    }
}

-(void)multiTabAction{
    
    
    if(delegate && [delegate respondsToSelector:@selector(onPressMultiTabButton)]){
        [delegate onPressMultiTabButton];
    }
    [self swapState];
    
    
    
}
-(void)viewFavouriteAction{
    if(delegate && [delegate respondsToSelector:@selector(onPressViewFavouriteButton)]){
        [delegate onPressViewFavouriteButton];
    }
}

-(void)fullScreenAction{
    if(currentState==TB_Normal){
        if(delegate && [delegate respondsToSelector:@selector(onPressFullScreenButton)]){
            [delegate onPressFullScreenButton];
        }
    }else{
        if(delegate && [delegate respondsToSelector:@selector(onPressNewTabButton)]){
            [delegate onPressNewTabButton];
        }
    }
}
-(void)shareAction{
    if(delegate && [delegate respondsToSelector:@selector(onPressShareButton)]){
        [delegate onPressShareButton];
    }
}

-(void)showMenuAction{
    if(delegate && [delegate respondsToSelector:@selector(onPressShowMenuButton)]){
        [delegate onPressShowMenuButton];
    }
}
-(BOOL)isOnTabState{
    return currentState == TB_Tab;
}
-(void)swapState{
    if(currentState==TB_Normal){
        currentState=TB_Tab;
        //forwardButton.hidden=YES;

        viewFavouriteButton.hidden=YES;
        searchTextButton.hidden=YES;
        showMenuButton.hidden=YES;
        multiTabButton.hidden=YES;
        shareButton.hidden=YES;
        [backButton setImage:[UIImage imageNamed:@"ic_browser_tabwindow_back_nor.png"] forState:UIControlStateNormal];
        [backButton setImage:nil forState:UIControlStateDisabled];
        [fullScreenButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_newpage_nor.png"] forState:UIControlStateNormal];
        [forwardButton setImage:[UIImage imageNamed:@"ic_menu_desk_clock.png"] forState:UIControlStateNormal];
        [forwardButton setImage:nil forState:UIControlStateDisabled];
    }else{
        currentState=TB_Normal;
        forwardButton.hidden=NO;
        viewFavouriteButton.hidden=NO;
        searchTextButton.hidden=NO;
        showMenuButton.hidden=NO;
        multiTabButton.hidden=NO;
        shareButton.hidden=NO;
        [backButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_bw_nor.png"] forState:UIControlStateNormal];
        [backButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_bw_dis.png"] forState:UIControlStateDisabled];
        [fullScreenButton setImage:[UIImage imageNamed:@"ic_browser_menu_fullscreen_nor.png"] forState:UIControlStateNormal];
        [forwardButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_fw_nor.png"] forState:UIControlStateNormal];
        [forwardButton setImage:[UIImage imageNamed:@"ic_browser_toolbar_fw_dis.png"] forState:UIControlStateDisabled];
    }
    [self activate:NO];
}
-(void)activate:(BOOL)_value{
    if(_value == NO){
        backButton.enabled=forwardButton.enabled=multiTabButton.enabled=viewFavouriteButton.enabled=searchTextButton.enabled=_value;
    }else{
        multiTabButton.enabled=viewFavouriteButton.enabled=searchTextButton.enabled=_value;
        if(currentState == TB_Tab){
            backButton.enabled = _value;
        }
    }
}
-(ZingToolBarState)getCurrentState{
    return currentState;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
